import { systemServer } from '@/utils/apiConfig'
import { deepObjClone } from '@/utils/index'
import api from '@/api/index'
export const DicsObj = {
  workStatus: '005', // 工作流状态
  areaCode: '101', // 区域编码
  department: '102', // 部门
  professionalTitles: '103', // 职称
  mechanism: '104', // 债权人
  debtNature: '105', // 债务性质
  debtUse: '106', // 债务用途
  debtType: '107', // 债务类别
  toDoListType: '006', // 待办类型
  dataType: '111', // 统计维度
  enterpriseType: '109' // 企业类型
}
//是否使用集团数据
export const statisticsCorporationDataDics = [
  { code: false, name: '否' },
  { code: true, name: '是' }
]
//单元格对齐方式
export const cellAlignmentDics = [
  { code: 'left', name: '左对齐' },
  { code: 'right', name: '右对齐' },
  { code: 'center', name: '居中' }
]
//单元格使用状态
export const cellStatusDics = [
  { code: '0', name: '不使用' },
  { code: '1', name: '使用' }
]
//单元格是否小计
export const cellIsSumDics = [
  { code: '0', name: '否' },
  { code: '1', name: '是' }
]
//单元格小计位置
export const cellSumPositionDics = [
  { code: '0', name: '前置' },
  { code: '1', name: '后置' }
]
// 审批结果
export const approveResultDics = [
  { code: '0', name: '拒绝' },
  { code: '1', name: '通过' }
]
// 用户类型
export const userTypeDics = [
  { code: '0', name: '区划管理' },
  { code: '1', name: '总后台' }
]
// 用户状态
export const userStatusDics = [
  { code: '1', name: '有效' },
  { code: '0', name: '无效' }
]
// 用户有效期
export const validityTypeDics = [
  { code: '1', name: '长期' },
  { code: '0', name: '短期' }
]
// 字典使用状态
export const dicStatusDics = [
  { code: '0', name: '不使用' },
  { code: '1', name: '使用' }
]
// 父级编码前缀启用
export const parentIdPrefixDics = [
  { code: '0', name: '不启用' },
  { code: '1', name: '启用' }
]
// 数据权限限制
export const permissionDataLimitDics = [
  { code: '0', name: '不限制' },
  { code: '1', name: '限制' }
]
// 菜单类型
export const menuTypeDics = [
  { code: '1', name: '应用' },
  { code: '2', name: '菜单' },
  { code: '3', name: '按钮' }
]
export const getDicsList = (code: string, needValue = false, params = { isAllChild: 0 }): any => {
  const url = `${systemServer}/dictionary/${code}/itemValue`
  return new Promise((resolve, reject) => {
    api.get<any>(url, { status: '1', ...params }).then((res) => {
      if (res.data && res.data.length > 0) {
        resolve(setChildDics(res.data, needValue))
      } else {
        resolve([])
      }
    })
  })
}
// 递归处理字典
const setChildDics = (treeList: any[], needValue: boolean): any[] => {
  return treeList.map((v: any) => ({
    code: needValue ? v.dictionaryValue : v.dictionaryCode,
    name: v.dictionaryName,
    children: v?.dictionaryChildLists?.length > 0 ? setChildDics(v.dictionaryChildLists, needValue) : []
  }))
}
/** 处理searchItem获取字典下拉
 * @param searchItem config里维护的searchItem
 * @param obj {属性名:字典}如{rfType:'DD010'}
 * @returns copyItem searchItem的深拷贝
 */
export async function initSearchItem(searchItem: any, obj: any) {
  const copyItem = deepObjClone(searchItem)
  if (!obj) return
  const promise = copyItem
    .filter((v: any) => v.type == 'select')
    .map(async (v: any) => {
      if (obj[v.prop]) {
        v.selectOptions = await getDicsList(obj[v.prop])
      }
    })
  await Promise.all(promise)
  return copyItem
}
